<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 查询添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
            @clear="getOrderList"
            @keyup.enter.native="getOrderList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 列表区域 -->
      <el-table :data="orderList" style="width: 100%" border stripe>
        <el-table-column type="index" label="索引"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column label="是否付款">
          <template v-slot="scope">
            <el-tag v-if="scope.row.pay_status === '0'" type="danger">未付款</el-tag>
            <el-tag v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column label="下单时间">
          <template v-slot="scope">{{scope.row.create_time | dateFormat('YYYY-MM-DD hh:mm:ss')}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-tooltip :enterable="false" effect="dark" content="修改地址" placement="bottom">
              <el-button
                type="primary"
                class="el-icon-edit"
                size="mini"
                circle
                @click="edit(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip :enterable="false" effect="dark" content="物流地址" placement="bottom">
              <el-button
                type="success"
                class="el-icon-location-outline"
                size="mini"
                circle
                @click="showProgress(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[ 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>

      <!-- 修改地址对话框 -->
      <el-dialog
        title="修改地址"
        :visible.sync="editAddrDialogVisible"
        width="70%"
        @close="closeEditAddrForm"
      >
        <el-form
          :rules="editAddrFormRules"
          ref="editAddrFormRef"
          :model="editAddrForm"
          label-width="100px"
        >
          <el-form-item label="省市区/县" prop="province">
            <el-cascader
              v-model="editAddrForm.province"
              :options="cityData"
              :props="{ expandTrigger: 'hover' }"
              @change="handleChange"
              :clearable="true"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="sureAddress">
            <el-input v-model="editAddrForm.sureAddress"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editAddrDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editAddrDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 物流状态对话框 -->
      <el-dialog title="物流进度" :visible.sync="progressDialogVisible" width="70%">
        <div class="radio">
          排序：
          <el-radio-group v-model="reverse">
            <el-radio :label="true">倒序</el-radio>
            <el-radio :label="false">正序</el-radio>
          </el-radio-group>
        </div>
        <el-timeline :reverse="reverse">
          <el-timeline-item
            v-for="(activity, index) in progressForm"
            :key="index"
            :timestamp="activity.time"
          >{{activity.context}}</el-timeline-item>
        </el-timeline>
        <span slot="footer" class="dialog-footer">
          <el-button @click="progressDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="progressDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import cityData from './citydata.js'
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      orderList: [],
      // 编辑对话框
      editAddrDialogVisible: false,
      // 编辑表单
      editAddrForm: {
        province: [],
        sureAddress: ''
      },
      // 编辑表单规则
      editAddrFormRules: {
        province: [
          { required: true, message: '请输入省市区/县名', trigger: 'blur' }
        ],
        sureAddress: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 省市区联动
      cityData,
      // 物流地址对话框开关
      progressDialogVisible: false,
      // 物流地址
      progressForm: [],
      // 时间线顺序
      reverse: false
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    async getOrderList () {
      const res = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 改变每页数量
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 改变当前页
    handleCurrentChange (newPagenum) {
      this.queryInfo.pagenum = newPagenum
      this.getOrderList()
    },
    // 编辑当前订单
    edit (item) {
      this.editAddrDialogVisible = true
    },
    // 物流信息
    async showProgress (item) {
      const res = await this.$http.get('/kuaidi/' + '1106975712662')
      if (res.meta.status !== 200) return this.$message.error(res.meta.message)
      this.progressForm = res.data

      this.progressDialogVisible = true
    },
    // 省市区联动改变
    handleChange (value) {
      console.log(this.editAddrForm)
    },
    // 关闭编辑表单
    closeEditAddrForm () {
      this.$refs.editAddrFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
.el-timeline {
  margin-top:30px;
}
</style>
